/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.views.ChangePasswordJPanelView;

/**
 *
 * @author TruongLQ
 */
public class ChangePasswordController {
    
    private final ChangePasswordJPanelView changePassJpnl;
    public ChangePasswordController(javax.swing.JPanel jPanel) {
        this.changePassJpnl = (ChangePasswordJPanelView)jPanel;
    }
    public boolean isValidInputs() {
        if (changePassJpnl.getTxtUsername().getText().isEmpty()) {
            MessageDialogUtils.dialogError(changePassJpnl, "User name must not be blank!");
            changePassJpnl.getTxtUsername().requestFocus();
            return false;
        } 
        if (changePassJpnl.getTxtPassword().getPassword().length ==0) {
            MessageDialogUtils.dialogError(changePassJpnl, "Old password must not be blank!");
            changePassJpnl.getTxtPassword().requestFocus();
            return false;
        } 
        
        if (changePassJpnl.getTxtNewPass().getPassword().length ==0) {
            MessageDialogUtils.dialogError(changePassJpnl, "New password must not be blank!");
            changePassJpnl.getTxtNewPass().requestFocus();
            return false;
        } 
        if (changePassJpnl.getTxtConfirmNewPass().getPassword().length ==0) {
            MessageDialogUtils.dialogError(changePassJpnl, "You have to confirm your new password!");
            changePassJpnl.getTxtConfirmNewPass().requestFocus();
            return false;
        }
        
        if (!String.valueOf(changePassJpnl.getTxtNewPass().getPassword()).equals(
                String.valueOf(changePassJpnl.getTxtConfirmNewPass().getPassword()))) {
            MessageDialogUtils.dialogError(changePassJpnl, "New password does not match!");
            changePassJpnl.getTxtConfirmNewPass().setText("");
            changePassJpnl.getTxtConfirmNewPass().requestFocus();
            return false;
        }
        
        if (String.valueOf(changePassJpnl.getTxtPassword().getPassword()).equals(
                String.valueOf(changePassJpnl.getTxtNewPass().getPassword()))) {
            MessageDialogUtils.dialogError(changePassJpnl, "New password must be different from old password!");
            changePassJpnl.getTxtNewPass().setText("");
            changePassJpnl.getTxtConfirmNewPass().setText("");
            changePassJpnl.getTxtNewPass().requestFocus();
            return false;
        }
        
        return true;
    }
    
    public void clearChangePasswordJPanel() {
        changePassJpnl.getTxtUsername().setText("");
        changePassJpnl.getTxtPassword().setText("");
        changePassJpnl.getTxtNewPass().setText("");
        changePassJpnl.getTxtConfirmNewPass().setText("");
    }
}
